const https = require('https');
const url = require('url');

// 测试微信登录API
async function testWechatLogin() {
  try {
    // 模拟微信小程序登录code
    const testCode = 'test-code';
    
    // 测试API端点
    const apiUrl = 'http://localhost:3000/api/auth/wechat-login';
    
    // 发送请求
    const postData = JSON.stringify({ code: testCode });
    
    const options = {
      hostname: 'localhost',
      port: 3000,
      path: '/api/auth/wechat-login',
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Content-Length': Buffer.byteLength(postData)
      }
    };
    
    const req = https.request(options, (res) => {
      let data = '';
      
      res.on('data', (chunk) => {
        data += chunk;
      });
      
      res.on('end', () => {
        console.log('Response:', data);
        try {
          const jsonData = JSON.parse(data);
          if (jsonData.success) {
            console.log('登录成功!');
            console.log('用户信息:', jsonData.data.user);
            console.log('Token:', jsonData.data.token);
          } else {
            console.log('登录失败:', jsonData.message);
          }
        } catch (error) {
          console.error('解析响应失败:', error);
        }
      });
    });
    
    req.on('error', (error) => {
      console.error('请求失败:', error);
    });
    
    req.write(postData);
    req.end();
    
  } catch (error) {
    console.error('测试失败:', error);
  }
}

// 运行测试
console.log('测试微信登录API...');
testWechatLogin();